/*
 * Created on 29.08.2007
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package at.fhv.dcs.aw.j2me.lcdui.lowlevel.sort;

import java.util.Random;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class SortingCanvas extends Canvas implements Runnable {
    public static final int WHITE = 0xffffff;
    public static final int BLACK = 0x0;
    
    protected int[] m_values; //values to be sorted
    protected Random m_rnd;
    protected Thread m_thread;
    
    
    public SortingCanvas() {
        initValues();
        
        //Emulator returns true in any case?!
        System.out.println("Is double buffered = " + isDoubleBuffered());
    }

    private void initValues() {
        //fill array with values: 0 <= value < width /2
        int height = getHeight();
        int width = getWidth() / 4;
        
        m_values = new int[height];
        m_rnd = new Random();
        
        for (int i = 0; i < m_values.length; i++) {
            m_values[i] = m_rnd.nextInt() % width + width;
        }
    }
    
    /* make sure double buffering is disabled in emulator */
    protected void paint(Graphics g) {
        int mid = getWidth() / 2;
        
        //clear area
        g.setColor(WHITE);
        g.fillRect(0, 0, getWidth(), getHeight());
        
        g.setColor(BLACK);
        synchronized (m_values) {
            for (int i = 0; i < m_values.length; i++) {
                g.drawLine(mid - m_values[i], i , mid + m_values[i], i);
            }
        }
    }

    public void run() {
        /*
        int min = 0;
        while (true) {            
            for (int i = 0; i < m_values.length - 1; i++ ) {
                min = i;
                for (int j = i + 1; j < m_values.length; j++) {
                    if (m_values[j] < m_values[i]) {
                        min = j;
                    }
                }
                
                synchronized(m_values) {
                    // swap the values
                    int tmp = m_values[min];
                    m_values[min] = m_values[i];
                    m_values[i] = tmp;
                }
                
                repaint();
            }
            
            if (m_thread != Thread.currentThread())  { //check for end
                return;
            }
            
            //initValues();
        }
        */
        //implement bubble sort
        while (true) {
            for (int i = m_values.length - 1; i >= 0; i--) {
                for (int j = 0; j < i; j++) {
                    synchronized (m_values) {
                        if (m_values[j] > m_values[j + 1]) {
                            int tmp = m_values[j];
                            m_values[j] = m_values[j + 1];
                            m_values[j + 1] = tmp;
                            
                            repaint();
                        }
                        
                    }
                }
            }
            
            if (m_thread != Thread.currentThread())  { //check for end
                return;
            }
        }
    }
    
    public void start() {
        m_thread = new Thread(this);
        m_thread.start();
    }
    
    public void stop() {
        if (m_thread != null) {
            Thread child = m_thread;
            m_thread = null;
            
            try {
                child.join(); //wait for thread termination
            } catch (InterruptedException e) {               
            }
        }
    }
}
